package cn.xx.pet.cotroller;

import cn.xx.basic.util.AjaxResult;
import cn.xx.basic.util.PageList;
import cn.xx.org.domain.Department;
import cn.xx.org.domain.Shop;
import cn.xx.pet.domain.MasterMsg;
import cn.xx.pet.domain.Pet;
import cn.xx.pet.domain.PetType;
import cn.xx.pet.mapper.PetMapper;
import cn.xx.pet.query.PetQuery;
import cn.xx.pet.service.IPetService;
import cn.xx.product.domain.Product;
import cn.xx.product.query.ProductQuery;
import cn.xx.user.domain.User;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @Autowired
    private PetMapper petMapper;

    //单个商品详情展示
    @GetMapping("/{id}")
    public AjaxResult pageQueryOne(@PathVariable("id") Long id) {
        Pet pet = petMapper.findById(id);
        return AjaxResult.me().setResultObject(pet);
    }

    // 批量上架
    @PutMapping("/onSale")
    public AjaxResult onSale(@RequestBody Long[] ids ) {
        try {
            petService.onSale(ids);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    // 批量下架
    @PutMapping("/offSale")
    public AjaxResult offSale(@RequestBody Long[] ids ) {
        try {
            petService.offSale(ids);
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

    //分页查询接口
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "")
    public PageList<Pet> pageQuery(@RequestBody PetQuery petQuery) {
        return petService.pageQuery(petQuery);
    }

    //分页列表展示接口
    @PostMapping("/list")
    public PageList<Pet> pageList(@RequestBody PetQuery petQuery) {
        petQuery.setState(1);
        return petService.pageQuery(petQuery);
    }

    //加载商铺
    @GetMapping("/shops")
    public List<Shop> getShops() {
        return petService.findShops();
    }

    //加载上任主人
    @GetMapping("/masters")
    public List<MasterMsg> getUsers() {
        return petService.findMasters();
    }

    //加载宠物类型
    @GetMapping("/types")
    public List<PetType> getTypes() {
        return petService.findTypes();
    }

    // 修改或保存
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Pet pet) {
        try {
            if (pet.getId() == null) {
                petService.save(pet);
            } else {
                petService.update(pet);
            }
            return AjaxResult.me().setSuccess(true).setMsg("操作完成了哦");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("操作失败").setResultObject("原因:" + e.getMessage());
        }
    }

}
